package com.java2007.web.controller;

import com.java2007.pojo.BookInfo;
import com.java2007.service.BookInfoService;
import com.java2007.service.ClassInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;
import java.util.Map;

/**
 * @author zy
 * @version V1.0
 * @date 2021/03/05
 */
@Controller
@RequestMapping("/bookinfo")
@Slf4j
public class BookInfoController {

    @Autowired
    private BookInfoService bookInfoService;

    @Autowired
    private ClassInfoService classInfoService;

    /**
     * 后台管理：显示全部图书信息
     * @return
     */
    @GetMapping("/list")
    public ModelAndView list(){
        List<BookInfo> bookInfoList = bookInfoService.findAll();
        ModelAndView mav = new ModelAndView("bookinfo-list");
        mav.addObject("bookInfoList",bookInfoList);
        return mav;
    }

    /**
     * 后台管理：显示选中图书的详细信息
     * @param bookInfoId
     * @return
     */
    @GetMapping({"/detail.html"})
    public ModelAndView detail(Long bookInfoId){
        log.info("BookController detail....{}", bookInfoId);

        BookInfo bookInfo = bookInfoService.findClassInfoAndBookInfoByBookInfoId(bookInfoId);
        ModelAndView mav = new ModelAndView("bookinfo-detail");
        mav.addObject("bookInfo",bookInfo);
        return mav;
    }

    /**
     * 后台管理：搜索图书
     * @param searchWord
     * @return
     */
    @PostMapping({"/search"})
    public ModelAndView search(String searchWord){
        log.info("BookController search....{}", searchWord);

        List<BookInfo> bookInfoList = bookInfoService.search(searchWord);
        ModelAndView mav = new ModelAndView("bookinfo-list");
        mav.addObject("bookInfoList",bookInfoList);
        mav.addObject("searchWord",searchWord);
        return mav;
    }

    /**
     * 后台管理：删除选中图书的信息
     * @param bookInfoId
     * @return
     */
    @GetMapping({"/delete.html"})
    public ModelAndView delete(Long bookInfoId){
        log.info("BookController delete....{}", bookInfoId);
        bookInfoService.deleteByBookInfoId(bookInfoId);
        ModelAndView mav = new ModelAndView("redirect:/bookinfo/list");
        mav.addObject("succ","删除成功");
        return mav;
    }

    /**
     * 后台管理：回显选中图书的相关信息
     * @param bookInfoId
     * @return
     */
    @GetMapping("/update.html")
    public ModelAndView updateUI(Long bookInfoId){
        Map<String, Object> map = bookInfoService.findByBookInfoId(bookInfoId);
        ModelAndView mav = new ModelAndView("bookinfo-update");
        mav.addObject("bookInfo", map.get("bookInfo"));
        mav.addObject("classInfoList",map.get("classInfoList"));
        return mav;
    }

    /**
     * 后台管理：编辑选中图书的相关信息
     * @param bookInfo
     * @return
     */
    @PostMapping("/edit.html")
    public ModelAndView edit(BookInfo bookInfo){
        log.info("edit bookInfo->{}", bookInfo);
        ModelAndView mav = new ModelAndView("redirect:/bookinfo/list");
        bookInfoService.edit(bookInfo);
        mav.addObject("succ","更新成功");
        return mav;
    }

    /**
     * 后台管理：显示增加图书信息页
     * @param bookInfo
     * @return
     */
    @GetMapping("/addui")
    public ModelAndView addUI(BookInfo bookInfo){
        ModelAndView mav = new ModelAndView("bookinfo-add");
        return mav;
    }

    /**
     * 后台管理：增加图书相关信息
     * @param bookInfo
     * @return
     */
    @PostMapping("/add")
    public ModelAndView add(BookInfo bookInfo){
        log.info("add bookInfo->{}", bookInfo);
        bookInfoService.add(bookInfo);
        ModelAndView mav = new ModelAndView("redirect:/bookinfo/list");
        mav.addObject("succ","增加成功");
        return mav;
    }


}
